﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace DACNPM.MyUserControl.UCHienThi
{
    
    public partial class UCDanhSachNCC : UserControl
    {
        String id_NCC;
        NhaCungCapBUO ncc = new NhaCungCapBUO();
        public UCDanhSachNCC()
        {
            InitializeComponent();
            dataGridView1.DataSource = ncc.GetAll();
        }

        
        private void rdTimTheoTenNCC_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaNCC.Enabled = false;
            txtTimTheoMaNCC.Text = "";
            txtTimTheoTenNCC.Enabled = true;
        }

        private void rdTimMaNCC_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaNCC.Enabled = true;
            txtTimTheoTenNCC.Enabled = false;
            txtTimTheoTenNCC.Text = "";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            
            if (rdTimMaNCC.Checked==true)
            {
                dataGridView1.DataSource = ncc.GetAll();
                dataGridView1.DataSource = ncc.SearchByMaNCC(txtTimTheoMaNCC.Text);
            }
            if (rdTimTheoTenNCC.Checked==true)
            {
                dataGridView1.DataSource = ncc.GetAll();
                dataGridView1.DataSource = ncc.SearchByTenNCC(txtTimTheoTenNCC.Text);
            }
        }

        private void txtTimTheoTenNCC_KeyPress(object sender, KeyPressEventArgs e)
        {
            dataGridView1.DataSource = ncc.SearchByTenNCC(txtTimTheoTenNCC.Text);
        }

        private void txtTimTheoTenNCC_KeyDown(object sender, KeyEventArgs e)
        {
            dataGridView1.DataSource = ncc.SearchByTenNCC(txtTimTheoTenNCC.Text);
        }

        private void txtTimTheoMaNCC_KeyPress(object sender, KeyPressEventArgs e)
        {
            dataGridView1.DataSource = ncc.SearchByMaNCC(txtTimTheoMaNCC.Text);
        }

        private void txtTimTheoMaNCC_KeyDown(object sender, KeyEventArgs e)
        {
            dataGridView1.DataSource = ncc.SearchByMaNCC(txtTimTheoMaNCC.Text);
        }

        private void thêmNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemNCC ucthemncc = new UCThemNCC();
            Form form=this.ParentForm;
            ControlAccess.ShowConTrol(form, ucthemncc, "grbHienThiChucNang");

        }

        private void sửaNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaNCC ucSuaNCC = new UCSuaNCC(id_NCC);
            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, ucSuaNCC, "grbHienThiChucNang");
        }
        //xóa nhà cung cấp
        private void xóaNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NhaCungCapBUO nccBUO = new NhaCungCapBUO();

            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (nccBUO.Delete(id_NCC))
                {
                    MessageBox.Show("Xóa thành công","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = ncc.GetAll();
            }
            
        }
        /// <summary>
        /// cho phép click chuột phải và lấy id
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        id_NCC = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }
    }
}
